define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reportbox/index',
                    add_url: 'reportbox/add',
                    edit_url: 'reportbox/edit',
                    del_url: 'reportbox/del',
                    multi_url: 'reportbox/multi',
                    table: 'user_box',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_box_id',
                sortName: 'user_box_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'box_no', title: __('盒子编号')},
                        {
                            field: 'city_info',
                            title: __('所属区域'),
                            formatter: Controller.api.formatter.city_info,
                            align: 'left'
                        },

                        {
                            field: 'house_info',
                            title: __('所属小区'),
                            formatter: Controller.api.formatter.house_info,
                            align: 'left'
                        },
                        {field: 'replen_num', title: __('补货次数')},
                        {field: 'total_price', title: __('补/配货进价')},
                        {field: 'order_num', title: __('订单数')},
                        {field: 'order_price', title: __('订单金额')},
                        {field: 'pv', title: __('PV')},
                        {field: 'uv', title: __('UV')},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        }
                    ]
                ]
            });
            //分配用户
            $(document).on("click", ".btn-xq", function () {
                var ids = $(this).data("id");
                var url = $(this).data("url");
                var title = $(this).data("title");
                Backend.api.open(url + '/ids/' + ids, title, {area: ["90%", "90%"]}, {
                    callback: function (value) {
                        table.bootstrapTable('refresh', {});
                    },
                });

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        goodsdetail() {
            var table = $("#table");
            var ids = table.attr('ids');
            Table.api.init({
                extend: {
                    index_url: 'reportbox/goodsdetail?ids=' + ids,
                    table: 'user_box',
                }
            });



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'box_goods_id',
                sortName: 'box_goods_id',
                searchFormVisible: false,
                commonSearch: false,
                columns: [
                    [
                        {field: 'no', title: __('序号')},
                        {field: 'goods_title', title: __('商品名称')},
                        {field: 'brand_title', title: __('品牌')},
                        {field: 'cate_title', title: __('分类')},
                        {field: 'buy_num', sortable: true, title: __('购买数量')},
                        {field: 'total_price', sortable: true, title: __('消费金额')},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.goods_custom
                        }
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        orderdetail: function () { // 初始化表格参数配置
            var table = $("#table");
            var ids = table.attr('ids');
            Table.api.init({
                extend: {
                    index_url: 'reportbox/orderdetail?ids=' + ids,
                    table: 'order',
                }
            });



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order',
                sortName: 'order',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                pageSize: 100,
                queryParams: function (params) {
                    params.limit = 100;
                    return params;
                },
                columns: [
                    [
                        {field: 'day', title: __('时间')},
                        {field: 'order_num', title: __('订单数')},
                        {field: 'total_order_price', title: __('订单金额')},
                        {field: 'pv', title: __('PV')},
                        {field: 'uv', title: __('UV')}
                    ]
                ]
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    var html = '';
                    html += '<a href="javascript:;" class="text-info btn-xq" data-title="订单详情" data-id="' + row.user_box_id + '" data-url="reportbox/orderdetail">订单详情</a>';
                    html += '|<a href="javascript:;" class="text-info btn-xq" data-title="商品详情" data-id="' + row.user_box_id + '" data-url="reportbox/goodsdetail">商品详情</a>';
                    return html;
                }, city_info: function (value, row, index) {
                    var html = row.province_name + '-' + row.city_name + '-' + row.area_name;
                    return html;
                },
                house_info: function (value, row, index) {

                    var html = row.quarters_name + '-' + row.unit_name + '-' + row.room_name;
                    return html;
                },
                goods_custom: function (value, row, index) {

                    var html = '<a href="javascript:;" class="text-info btn-xq" data-title="走势图" data-id="' + row.box_goods_id + '" data-url="reportbox/orderdetail">走势图</a>';
                    return html;
                },
            },
        }
    };
    return Controller;
});